<template>
	<view class="home pageBg">
		<!-- 自定义导航栏 -->
		<custom-nav-bar title="推荐"></custom-nav-bar>

		<!-- 横幅 -->
		<view class="banner">
			<swiper indicator-dots indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff" autoplay
				interval="3000" duration="1000" circular>
				<swiper-item v-for="item in bannerList" :id="item._id">
					<!-- 用于在小程序中实现跳转到其他小程序的功能。 -->
					<navigator v-if="item.target == 'miniProgram'" :url="item.url" class="item" target="miniProgram"
						:app-id="item.appid">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>

					<!-- 跳转壁纸列表 -->
					<navigator v-else :url="`/pages/classlist/classlist?${item.url}`" class="item">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>

		<!-- 公告 -->
		<view class="notice">
			<div class="icon">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<view class="text">公告</view>
			</div>
			<div class="content">
				<swiper :autoplay="true" interval="1500" circular vertical>
					<!-- 跳转公告详情 -->
					<swiper-item v-for="item in noticeList" :id="item._id">
						<navigator :url="'/pages/notice/detail?id='+item._id">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</div>
		</view>

		<!-- 每日推荐 -->
		<view class="recommendation">
			<!-- 标题 -->
			<common-title>
				<template #title>
					每日推荐
				</template>
				<!-- 自定义插槽 -->
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="18"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="M月dd日"></uni-dateformat>
						</view>
					</view>
				</template>
			</common-title>
			<!-- 推荐图片 -->
			<view class="recommendation-content">
				<scroll-view scroll-x>
					<navigator :url="`/pages/preview/preview?id=`+item._id+`&type=index`" class="item"
						v-for="item in recommendList" :id="item._id">
						<image :src="item.smallPicurl" mode="aspectFit"></image>
					</navigator>
				</scroll-view>
			</view>
		</view>

		<!-- 专题精选 -->
		<view class="theme">
			<!-- 标题 -->
			<common-title>
				<template #title>
					专题精选
				</template>
				<!-- 自定义插槽 -->
				<template #custom>
					<navigator url="" class="more">More+</navigator>
				</template>
			</common-title>

			<!-- 主题分类 -->
			<view class="theme-content">
				<theme-item v-for="item in classifyList" :id="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app';
	import {
		getBannerApi,
		getClassifyApi,
		getDailyRecommendApi,
		getNoticeApi
	} from '../../api/apis';

	const bannerList = ref([])
	const noticeList = ref([])
	const recommendList = ref([])
	const classifyList = ref([])

	async function getBannerData() {
		const res = await getBannerApi()
		bannerList.value = res.data
	}

	async function getNoticeData() {
		const res = await getNoticeApi()
		noticeList.value = res.data
	}

	async function getDailyRecommendData() {
		const res = await getDailyRecommendApi()
		recommendList.value = res.data
	}

	async function getClassifyData() {
		const res = await getClassifyApi({
			select: true
		})
		classifyList.value = res.data
	}

	onLoad(async () => {
		uni.showLoading({
			title: "加载中"
		})
		await Promise.all([getBannerData(), getNoticeData(), getDailyRecommendData(), getClassifyData()])
		uni.setStorageSync('storeIndexWallList', recommendList.value)
		uni.hideLoading()
	})
	onUnload(() => {
		uni.removeStorageSync("storeIndexWallList")
	})
</script>

<style lang="scss">
	.home {
		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				width: 750rpx;
				height: 340rpx;

				&-item {
					width: 100%;
					height: 100%;
					padding: 0 30rpx;

					.item {
						width: 100%;
						height: 100%;

						image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}
				}
			}
		}

		.notice {
			display: flex;
			width: 690rpx;
			margin: 0 auto;
			height: 80rpx;
			border-radius: 40rpx;
			background: #f9f9f9;

			.icon {
				width: 140rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				:deep() {
					.uni-icons {
						color: $brand-theme-color !important;
					}
				}

				.text {
					color: $brand-theme-color;
					font-weight: 600; //字体粗细
					font-size: 28rpx; //字体大小
				}
			}

			.content {
				flex: 1;

				swiper {
					height: 100%;

					&-item navigator {
						height: 100%;
						line-height: 80rpx;
						font-size: 30rpx;
						color: #666;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis; //...取代多余的值
					}
				}
			}
		}

		.recommendation {
			padding-top: 50rpx;

			.date {
				color: #28b389;
				display: flex;
				align-items: center;

				:deep() {
					.uni-icons {
						color: #28b389 !important;
					}
				}

				.text {
					margin-left: 5rpx;
				}
			}

			&-content {
				width: 720rpx;
				margin-left: 30rpx;
				margin-top: 30rpx;

				scroll-view,
				uni-scroll-view-content {
					white-space: nowrap; //不换行

					.item {
						width: 200rpx;
						height: 430rpx;
						display: inline-block;
						margin-right: 15rpx;

						image {
							width: 100%;
							height: 100%;
							border-radius: 20rpx;
						}

						:last-child {
							//item最后一个子元素
							margin-right: 30rpx;
						}
					}
				}
			}
		}

		.theme {
			padding: 50rpx 0;

			.more {
				font-size: 32rpx;
				color: #888;
			}

			&-content {
				display: grid;
				margin-top: 30rpx;
				padding: 0 30rpx;
				grid-template-columns: repeat(3, 1fr);
				gap: 15rpx;
			}
		}

	}
</style>